<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Information;
use app\common\model\Nav;
use app\common\model\Record;


class Index
{
    /**
     * 首页
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'topnav' => $this->topnav(),   //获取首页--顶部导航
            'information' => $this->information(),   //获取首页--网站基础设置
            'banner' => $this->banner(),   //获取首页--banner
            'bomnav' => $this->bomnav(),  //获取首页--底部导航
            'exam' => $this->exam(),  //获取首页--立即考试和考试入口时间
        ]);
    }


    /**
     * 首页顶部导航
     *
     * @return \think\Response
     */
    public function topnav()
    {
        $data = Nav::where(['type' => 1, 'status' => 1])
            ->field('name')
            ->limit(0, 5)
            ->order(['id' => 'asc'])
            ->select();
        return $data;
    }

    /**
     * 首页网站基础信息
     *
     * @return \think\Response
     */
    public function information()
    {
        $data = Information::where(['status'=>1])
            ->field('title,logo,image,address,number,phone,copyright')
            ->find();
        $data['image'] = config("app.domain_url") .str_replace('\\', '/', $data['image']);
        $data['logo'] = config("app.domain_url") .str_replace('\\', '/', $data['logo']);
        return $data;
    }

    /**
     * 首页轮播图
     *
     * @return \think\Response
     */
    public function banner()
    {
        $data = Banner::where(['type'=>1, 'status'=>1])
            ->field('image')
            ->withAttr('image',function ($val, $data){
                return strtolower(config("app.domain_url").str_replace('\\', '/', $val));
            })
            ->order(['id' => 'asc'])
            ->select();
        return $data;
    }

    /**
     * 首页底部导航
     *
     * @return \think\Response
     */
    public function bomnav()
    {
        $data = Nav::where(['type'=>2, 'status'=>1])
            ->field('name')
            ->order(['id' => 'asc'])
            ->find();
        return $data;
    }

    /**
     * 首页立即考试和考试入口时间
     *
     * @return \think\Response
     */
    public function exam()
    {
        $data = Record::where(['status'=>1])
            ->field('id,paper_id,exam_time,star_time')
            ->order(['id' => 'asc'])
            ->select();
        foreach ($data as &$value) {
            $value['paper_id'] = $value->paper->title;
            unset($value->paper);
            $value['star_time'] = date('Y-m-d H:i:s', $value['star_time']);

        }
        return $data;
    }


}

